//
//  TTCustomChooseLuoshiView.m
//  TaoTuo
//
//  Created by apple on 2020/11/25.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTCustomChooseLuoshiView.h"
#import "TTCustomLuoshiTableViewCell.h"
#import <UIView+TYAlertView.h>
#import "TTCustomChooseLuoshiModel.h"

@interface TTCustomChooseLuoshiView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
/// 我知道了
@property (nonatomic, strong) UIButton *knowBtn;

@end

@implementation TTCustomChooseLuoshiView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.page = 1;
        [self setupSubviews];
        
    }
    return self;
}

- (void)show
{
    [LLAnimationAlertView animationAlertViewWithView:self animationType:AnimationFromBottom];
    
}

- (void)dismiss
{
    [LLAnimationAlertView dismissView:self];
}

- (void)setType:(QueryGoodsType)type
{
    _type = type;
    self.titleLabel.text = @"从购物车挑选";
    
    [self getLuoshiListDatas];
    
    if (type == QueryGoodsTypeWithOrder) {
        self.titleLabel.text = @"从订单挑选";
        @weakify(self)
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self)
            self.page = 1;
            [self getLuoshiListDatas];
        }];
        
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @strongify(self)
            self.page++;
            [self getLuoshiListDatas];
        }];
        
    }
    
}

// 获取石头数据
- (void)getLuoshiListDatas
{
    NSString *urlStr = queryOrderInlay_api;
    NSDictionary *dic = @{
        @"param.inlayGoodsType" : @2,
        @"param.pageIndex" : @(self.page),
        @"param.pageSize" : @20
    };
    
    if (self.type == QueryGoodsTypeWithShoppingCart) {
        
        urlStr = queryShoppingCartInlay_api;
        dic = @{
            @"param.salesmanAccountId" : [TTLoginSingle sharedSingle].loginModel.SalesmanAccountId,
            @"param.goodsType" : @2,
        };
    }
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:urlStr parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        if (self.type == QueryGoodsTypeWithOrder && self.page == 1) {
            [self.dataArray removeAllObjects];
        }
        
        for (NSDictionary *dic in responseObject) {
            TTCustomChooseLuoshiModel *model = [TTCustomChooseLuoshiModel modelWithJSON:dic];
            [self.dataArray addObject:model];
        }
        
        if (self.type == QueryGoodsTypeWithOrder) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
        
        self.knowBtn.hidden = self.dataArray.count > 0;
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        @strongify(self)
        self.knowBtn.hidden = NO;
        if (self.type == QueryGoodsTypeWithOrder) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
    } showLoadingInView:self];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTCustomLuoshiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.detailModel = self.dataArray[indexPath.row];
    cell.backgroundColor = UIColor.clearColor;
    @weakify(self)
    cell.chooseFinishedBlock = ^(TTCustomChooseLuoshiModel * _Nonnull model) {
        @strongify(self)
        [[NSNotificationCenter defaultCenter] postNotificationName:@"customChooseLuoshiFinishedNotifyKey" object:model];
        [self dismiss];
    };
    return cell;
}

- (void)setupSubviews
{
    UIBezierPath *bgPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    bgLayer.path = bgPath.CGPath;
    bgLayer.fillColor = UIColorHex(#F6F6F6).CGColor;
    [self.layer addSublayer:bgLayer];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(100, 37)];
    [path addLineToPoint:CGPointMake(self.width - 100, 37)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.lineWidth = 2;
    layer.lineDashPattern = @[@2, @3];
    layer.lineCap = kCALineCapRound;
    layer.strokeColor = UIColorHex(#999999).CGColor;
    layer.path= path.CGPath;
    [self.layer addSublayer:layer];
    
    _titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#333333) font:[UIFont boldSystemFontOfSize:16]];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.backgroundColor = UIColorHex(#F6F6F6);
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(26);
        make.width.mas_equalTo(120);
        make.centerX.equalTo(self);
    }];
    
    [self tableView];
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColorHex(#F6F6F6);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyImageName = @"shop_cart_no_data";
        _tableView.emptyTitel = @"哎吆！老板您还没有可镶嵌的钻石";
        _tableView.verticalOffset = -50;
        _tableView.layer.cornerRadius = 10;
        _tableView.layer.masksToBounds = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 180;
        [_tableView registerClass:[TTCustomLuoshiTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = [UIView new];
        [self addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
            make.left.right.bottom.equalTo(self);
        }];
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UIButton *)knowBtn
{
    if (!_knowBtn) {
        _knowBtn = [UIButton createButtonWithTitle:@"我知道了" titleColor:UIColor.whiteColor titleFont:[UIFont systemFontOfSize:16] backgroundColor:UIColorHex(#8B57E6) cornerRadius:20 target:self action:@selector(dismiss)];
        _knowBtn.hidden = YES;
        [self addSubview:_knowBtn];
        [_knowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).mas_offset(-35);
            make.width.mas_equalTo(160);
            make.height.mas_equalTo(40);
            make.centerX.equalTo(self);
        }];
    }
    return _knowBtn;
}

@end
